Skip to content

HTTP高级请求 - HttpRequestEx

函数简介

高级HTTP请求,支持自定义Method、请求头(含Cookie)、请求体。

接口名称

HttpRequestEx

DLL调用

c
const char* HttpRequestEx(int64_t instance, const char* method, const char* url, const char* headers, const char* body, const char* content_type, int32_t* status_code);

参数说明

参数名类型说明
instance长整数型OLAPlug对象的指针,由 CreateCOLAPlugInterFace 接口生成。
method字符串方法,如"GET"/"POST"/"PUT"/"DELETE",大小写不敏感。
url字符串完整URL。
headers字符串自定义请求头,多行字符串,每行"Name: Value",可为空。
body字符串请求体,GET可传空。
content_type字符串如"application/json",可为空。
status_code整数型指针输出HTTP状态码,可为NULL。

使用场景

场景method关键参数
Bearer Token 鉴权GET/POSTheadersAuthorization: Bearer xxx
带 Cookie 请求GET/POSTheadersCookie: session=abc
REST 更新/删除PUT/DELETEbody + content_type
需要判断状态码任意传入 status_code 输出参数

headers 格式:每行 Name: Value,以 \r\n 分隔。示例:

Authorization: Bearer your_token_here
User-Agent: OLAPlug/1.0
Accept: application/json

完整示例见 网络使用指南

示例

SDK 调用

cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
// 自定义 Header(含 Bearer Token)
string headers =
    "Authorization: Bearer your_token_here\r\n"
    "User-Agent: OLAPlug/1.0\r\n"
    "Accept: application/json";
int status = 0;
string resp = ola.HttpRequestEx(
    "GET",
    "https://api.example.com/v1/user/profile",
    headers, "", "", status
);
// status 为 HTTP 状态码(200/401/404 等)
csharp
using OLAPlug;

var ola = new OLAPlugServer();
string headers = "Authorization: Bearer your_token_here\r\nUser-Agent: OLAPlug/1.0";
int status = 0;
string resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", headers, "", "", status);
python
from OLAPlugServer import OLAPlugServer

ola = OLAPlugServer()
headers = "Authorization: Bearer your_token_here\r\nUser-Agent: OLAPlug/1.0"
status = 0
resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", headers, "", "", status)
java
import com.olaplug.OLAPlugServer;

OLAPlugServer ola = new OLAPlugServer();
String headers = "Authorization: Bearer your_token_here\r\nUser-Agent: OLAPlug/1.0";
int status = 0;
String resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", headers, "", "", status);
go
import "github.com/ola/olaplug/olaplug"

ola, _ := olaplug.NewOLAPlugServer("OLAPlug_x64.dll")
defer ola.ReleaseObj()
status := 0
resp := ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status)
rust
use olaplug::OLAPlugServer;

let ola = OLAPlugServer::new("OLAPlug_x64.dll").unwrap();
let status = 0;
let resp = ola.http_request_ex("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", &status);
cpp
var ola = com("OlaPlug.OlaSoft")
var status = 0
var resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status)
vbscript
Set ola = CreateObject("OlaPlug.OlaSoft")
status = 0
resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status)
text
.局部变量 ola, OLAPlug
ola.创建 ()
status = 0
resp = ola.HttpRequestEx ("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token", "", "", status)
aardio
import OLAPlugServer;
var ola = OLAPlugServer();
var status = 0;
var resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);
text
变量 ola <类型 = OLAPlugServer>
ola = 新建 OLAPlugServer
整数 status = 0
文本型 resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token", "", "", status)
cpp
#include "OLAPlugServer.h"

OLAPlugServer ola;
int status = 0;
string resp = ola.HttpRequestEx("GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);

原生 DLL 调用

cpp
long instance = CreateCOLAPlugInterFace();
int status = 0;
long respPtr = HttpRequestEx(instance, "GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);
if (respPtr != 0) FreeStringPtr(respPtr);
csharp
using System.Runtime.InteropServices;
using System.Text;

[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long CreateCOLAPlugInterFace();
[DllImport("OLAPlug_x64.dll", CallingConvention = CallingConvention.StdCall)]
static extern long HttpRequestEx(long ola, string method, string url, string headers, string body, string content_type, ref int status_code);

long instance = CreateCOLAPlugInterFace();
int status = 0;
long respPtr = HttpRequestEx(instance, "GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);
if (respPtr != 0) FreeStringPtr(respPtr);
python
from ctypes import CDLL, c_int, c_int64, create_string_buffer

ola = CDLL("OLAPlug_x64.dll")
ola.CreateCOLAPlugInterFace.restype = c_int64
instance = ola.CreateCOLAPlugInterFace()
int status = 0;
long respPtr = HttpRequestEx(instance, "GET", "https://api.example.com/v1/user/profile", "Authorization: Bearer token\r\n", "", "", status);
if (respPtr != 0) FreeStringPtr(respPtr);

返回值

返回值说明
(返回值)字符串指针,返回响应体内容,失败返回NULL。需调用 FreeStringPtr 释放内存。

注意事项

项目说明
释放内存返回的字符串需要调用 FreeStringPtr 释放内存。
headers格式每行"Name: Value",以"\r\n"分隔。
支持的HTTP方法GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS等。
status_code可以为NULLstatus_code可以为NULL,如果不需要获取状态码。